package com.longcai.piaojia.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.longcai.piaojia.R;
import com.longcai.piaojia.activity.MyBagActivity;
import com.longcai.piaojia.conn.MyBagListGet;
import com.longcai.piaojia.conn.MyCollectionGet;
import com.longcai.piaojia.util.ZXingUtils;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.bound.BoundViewHelper;
import com.zcx.helper.glide.GlideLoader;
import com.zcx.helper.scale.ScaleScreenHelperFactory;

import java.util.List;

/**
 * Created by Administrator on 2016/9/19.
 */
public class MyBagAdapter extends ArrayAdapter<MyBagListGet.MyBagEntity> {

    private Context context;
    private LayoutInflater layoutInflater;

    public MyBagAdapter(Context context, List<MyBagListGet.MyBagEntity> objects) {

        super(context, 0, objects);

        this.context = context;

        layoutInflater = ((Activity) context).getLayoutInflater();
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = null;

        MyBagListGet.MyBagEntity myBagEntity = getItem(position);

        if (convertView == null) {

            convertView = BoundViewHelper.boundView(viewHolder = new ViewHolder(), ScaleScreenHelperFactory.getInstance().loadView((ViewGroup) layoutInflater.inflate(R.layout.item_mybag_list, null)));

            convertView.setTag(viewHolder);

        } else {

            viewHolder = (ViewHolder) convertView.getTag();

        }

        viewHolder.mybag_show_title_tv.setText(myBagEntity.title);
        viewHolder.mybag_show_time_tv.setText(myBagEntity.time);
        viewHolder.mybag_order_ticket_address_tv.setText(myBagEntity.ticket_address);
        viewHolder.mybag_order_address_tv.setText(myBagEntity.address);
        viewHolder.mybag_goods_count_tv.setText(myBagEntity.poll);
        viewHolder.mybag_redeem_code_tv.setText(myBagEntity.orderNo);

        GlideLoader.getInstance().get(context, myBagEntity.picUrl, viewHolder.item_mybag_list_iv, R.mipmap.goods);

        if(myBagEntity.status.equals("1")){

            viewHolder.item_mybag_list_status_tv.setTextColor(ContextCompat.getColor(context, R.color.mybag_used_color));
            viewHolder.item_mybag_list_status_tv.setText(context.getResources().getString(R.string.mybag_used_text));

        }else{

            viewHolder.item_mybag_list_status_tv.setTextColor(ContextCompat.getColor(context, R.color.mybag_no_used_color));
            viewHolder.item_mybag_list_status_tv.setText(context.getResources().getString(R.string.mybag_no_used_text));

        }

        viewHolder.item_mybag_list_qrcode_iv.setImageBitmap(ZXingUtils.creatBarcode(context, myBagEntity.orderNo, ScaleScreenHelperFactory.getInstance().getWidthHeight(408), ScaleScreenHelperFactory.getInstance().getWidthHeight(130), false));


        return convertView;

    }

    private class ViewHolder {

        @BoundView(R.id.mybag_show_title_tv) private TextView mybag_show_title_tv;
        @BoundView(R.id.mybag_show_time_tv) private TextView mybag_show_time_tv;
        @BoundView(R.id.mybag_order_ticket_address_tv) private TextView mybag_order_ticket_address_tv;
        @BoundView(R.id.mybag_order_address_tv) private TextView mybag_order_address_tv;
        @BoundView(R.id.mybag_goods_count_tv) private TextView mybag_goods_count_tv;
        @BoundView(R.id.mybag_redeem_code_tv) private TextView mybag_redeem_code_tv;
        @BoundView(R.id.item_mybag_list_status_tv) private TextView item_mybag_list_status_tv;
        @BoundView(R.id.item_mybag_list_iv) private ImageView item_mybag_list_iv;
        @BoundView(R.id.item_mybag_list_qrcode_iv) private ImageView item_mybag_list_qrcode_iv;

    }


}
